package com.fuluwa.interfaces.usercenter.facade;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import com.fuluwa.domain.bank.model.Account;
import com.fuluwa.domain.bank.model.Account.AccountType;
import com.fuluwa.domain.bank.service.AccountService;
import com.fuluwa.domain.security.model.User;
import com.fuluwa.domain.security.repository.UserRepository;
import com.fuluwa.domain.usercenter.model.Contact;
import com.fuluwa.domain.usercenter.model.UserBasicInfo;
import com.fuluwa.domain.usercenter.model.UserClass;
import com.fuluwa.domain.usercenter.model.UserProfile;
import com.fuluwa.domain.usercenter.model.competence.EnterpriseCareerLevelMapping;
import com.fuluwa.domain.usercenter.model.competence.MaterialLevelDefine;
import com.fuluwa.domain.usercenter.respository.EnterpriseCareerLevelMappingRepository;
import com.fuluwa.domain.usercenter.respository.MaterialLevelDefineRepository;
import com.fuluwa.domain.usercenter.respository.UserProfileRepository;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.jms.MessageProducer;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Component
public class UserServiceFacadeImpl implements UserServiceFacade {
	@Autowired
	UserRepository userRepository;
	@Autowired
	MessageProducer messageProducer;
	@Autowired
	UserProfileRepository userProfileRepository;
	
	@Autowired
	AccountService accountService;
	
	
	@Autowired
	EnterpriseCareerLevelMappingRepository careerLevelMappingRepository;
	@Autowired
	MaterialLevelDefineRepository levelDefineRepository;
	
	@Override
	@Transactional
	public void registerUser(UserDTO userDTO) throws PersistenceException {
		User user = userDTO.toUser();
//		user.getUserAttribute().save();
		user.save();
		
		UserClass c=new UserClass(1L);
//		UserClass c=new UserClass("Normal");  
//		c.save();
		
		UserProfile profile=new UserProfile(user, c);
		profile.save();
		
		Account cash=accountService.openingAccount(user, AccountType.CASH);
		Account gold=accountService.openingAccount(user, AccountType.GOLD);
		
		messageProducer.sendQueue(user);
	}

	@Override
	public boolean checkUserByLoginName(String loginName) {
		User user = userRepository.findByLoginName(loginName);
		return (user!=null);
	}

	@Override
	@Transactional
	public void changeUserPassword(String loginName, String oldPassword, String newPassword) throws PersistenceException {
		User user = userRepository.findByLoginName(loginName);
		user.updatePassword(newPassword);
		user.update();
	}

	public void setMessageProducer(MessageProducer messageProducer) {
		this.messageProducer = messageProducer;
	}

	@Override
	public String getUserNo(String loginName) {
		// TODO Auto-generated method stub
		User user = userRepository.findByLoginName(loginName);
		return userProfileRepository.findByUser(user).getUserNo();
	}

	@Override
	@Transactional
	public void updateContact(String userNo, Contact contact) throws PersistenceException {
		// TODO Auto-generated method stub
		UserProfile userprofile = userProfileRepository.findOne(userNo);
		userprofile.updateContact(contact);
	}

	@Override
	@Transactional
	public void updateUserBasicInfo(String userNo, UserBasicInfo userBasicInfo) throws PersistenceException {
		// TODO Auto-generated method stub
		UserProfile userprofile = userProfileRepository.findOne(userNo);
		userprofile.updateUserBasicInfo(userBasicInfo);
	}
	
	public Map<String,Object> getLists(){
		Map<String,Object> map=Maps.newHashMap();
		List<IdValueDTO> lds=Lists.newArrayList();
		List<MaterialLevelDefine> lvls = levelDefineRepository.findAll();
		for (MaterialLevelDefine materialLevelDefine : lvls) {
			lds.add(new IdValueDTO(materialLevelDefine.getId(),materialLevelDefine.getLevelType().getName()+">"+materialLevelDefine.getName()));
		}
		map.put("MaterialLevels", lds);
		List<IdValueDTO> evs=Lists.newArrayList();
		List<EnterpriseCareerLevelMapping> elvls = careerLevelMappingRepository.findAll();
		for (EnterpriseCareerLevelMapping m : elvls) {
			evs.add(new IdValueDTO(m.getId(),m.getEnterpriseLevelDefine().getName()+"->"+m.getLevelName()));
		}
		map.put("EnterpriseLevels", evs);
		return map;
	}

	@Override
	public String get(Long[] material,  Long level) {
		Iterable<MaterialLevelDefine> lvls = levelDefineRepository.findAll(Lists.newArrayList(material));
		float value=0;
		for (MaterialLevelDefine materialLevelDefine : lvls) {
			value+=materialLevelDefine.compute();
		}
		EnterpriseCareerLevelMapping findOne = careerLevelMappingRepository.findOne(level);
		String t="Our Career Level:"+findOne.getCareerLevelDefine().getLevelName()+",should have value:"+findOne.getCareerLevelDefine().getValue();
		t+="\n\n\nAccording the checkbox,computed value :"+value;
		
		return t;
	}
}
